package com.punai.dataanalysis.epdatakb.service.impl;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.utils.DateUtil;
import com.punai.common.utils.NumberUtil;
import com.punai.dataanalysis.epdatakb.bo.JtczResultBo;
import com.punai.dataanalysis.epdatakb.domain.DataAnalysisNdgclpdqkhzb;
import com.punai.dataanalysis.epdatakb.service.NG001change1212.NG001NdgccbqkhzbC1212Service;
import com.punai.dataanalysis.epdatakb.vo.DataAnalysisVo;
import com.punai.dataanalysis.ng001.bo.RgJxD;
import com.punai.dataanalysis.ng001.bo.YearLjWccz;
import com.punai.dataanalysis.ng001.domain.ProjectTable;
import com.punai.dataanalysis.ng001.mapper.ProjectTableMapper;
import com.punai.dataanalysis.ng001.mapper.YearGcQkMapper;
import com.punai.external.domain.PmProjectInfo;
import com.punai.external.mapper.PmProjectInfoMapper;
import com.punai.externalccb.domain.YdTime;
import com.punai.externalccb.domain.YdczSummary;
import com.punai.externalccb.mapper.YdCbfeeiteminfoStandardMapper;
import com.punai.externalccb.mapper.YdTimeMapper;
import com.punai.externalccb.mapper.YdczSummaryMapper;
import com.punai.externalccb.selectbo.CbTjBo;
import com.punai.framework.datasource.DynamicDataSourceContextHolder;
import com.punai.prodatalibrary.domain.LibProjectInfo;
import com.punai.prodatalibrary.domain.bo.GlobalProInfoBo;
import com.punai.prodatalibrary.mapper.LibProjectInfoMapper;
import com.punai.prodatalibrary.mapper.LibProjectMapper;
import com.punai.prodatalibrary.service.ILibProjectInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.dataanalysis.epdatakb.mapper.DataAnalysisNdgccbqkhzbMapper;
import com.punai.dataanalysis.epdatakb.domain.DataAnalysisNdgccbqkhzb;
import com.punai.dataanalysis.epdatakb.service.IDataAnalysisNdgccbqkhzbService;

/**
 * 年度工程成本情况汇总Service业务层处理
 *
 * @author zxw_
 * @date 2023-04-20
 */
@Service
public class DataAnalysisNdgccbqkhzbServiceImpl extends ServiceImpl<DataAnalysisNdgccbqkhzbMapper,DataAnalysisNdgccbqkhzb> implements IDataAnalysisNdgccbqkhzbService
{

    public static final Logger log = LoggerFactory.getLogger(IDataAnalysisNdgccbqkhzbService.class);
    @Autowired
    private DataAnalysisNdgccbqkhzbMapper dataAnalysisNdgccbqkhzbMapper;

    @Autowired
    private ProjectTableMapper projectTableMapper;

    @Autowired
    private ILibProjectInfoService libProjectInfoService;

    @Autowired
    private YearGcQkMapper yearGcQkMapper;

    @Autowired
    private NG001NdgccbqkhzbC1212Service ng001NdgccbqkhzbC1212Service;


    @Override
    public List<? extends BaseEntity> getDataList(DataAnalysisVo dataAnalysisVo) {

        if (isC1212) {
            List<? extends BaseEntity> dataList = ng001NdgccbqkhzbC1212Service.getDataList(dataAnalysisVo);
            return dataList;
        }

        List<GlobalProInfoBo> list = libProjectInfoService.selectInfoListNoPerm(new GlobalProInfoBo());

        List<String> pmInfoIds = list.stream().map(GlobalProInfoBo::getPmproinfoid).collect(Collectors.toList());
        List<ProjectTable> projectTables = projectTableMapper.selectNdgccbqkhzbListByVo(pmInfoIds);
        pmInfoIds = projectTables.stream().map(ProjectTable::getPhid).filter(Objects::nonNull).collect(Collectors.toList());
        if (CollUtil.isEmpty(pmInfoIds)) {
            return new ArrayList<>();
        }
        Map<String, ProjectTable> projectTablesMap = projectTables.stream().collect(Collectors.toMap(ProjectTable::getPhid, Function.identity(), (v1, v2) -> v2));
        List<YearLjWccz> yearLjWcczs = yearWccz(pmInfoIds);
        Map<String, YearLjWccz> yearLjWcczsMap = yearLjWcczs.stream().collect(Collectors.toMap(YearLjWccz::getPc, Function.identity(), (v1, v2) -> v2));
        List<RgJxD> rgJxDS = rgjxD(pmInfoIds);
        Map<String, RgJxD> rgJxDSMap = rgJxDS.stream().collect(Collectors.toMap(RgJxD::getPc, Function.identity(), (v1, v2) -> v2));


        List<DataAnalysisNdgccbqkhzb> res = new ArrayList<>();
        for (GlobalProInfoBo globalProInfoBo : list) {
            String pc = globalProInfoBo.getPmproinfoid();
            DataAnalysisNdgccbqkhzb qk = new DataAnalysisNdgccbqkhzb();
            if(StrUtil.isNotEmpty(globalProInfoBo.getPmproinfoid())){
                ProjectTable projectTable = projectTablesMap.get(pc);
                if (projectTable == null) {
                    continue;
                }
                YearLjWccz yearLjWccz = yearLjWcczsMap.get(pc);
                RgJxD rg = rgJxDSMap.get(pc);
                qk = DataAnalysisNdgccbqkhzb.conversion(qk,projectTable);
                qk.setYearCzPdHj(yearLjWccz.getPdzhj());
                qk.setYearCzSj(yearLjWccz.getSj());
                qk.setYearCzJgc(yearLjWccz.getJgc());
                qk.setYearCostRgfJe(rg.getRgf());
                qk.setYearCostClfJe(rg.getClf());
                qk.setYearCostJxfJe(rg.getJxf());
                qk.setYearCostQtzjfJe(rg.getQtzjf());
                qk.setYearCostJjfJe(rg.getJjfJe());
                qk.setYearCostJjfGlrygzJe(rg.getJjfGlgz());
                qk.setYearCostFbgczcJe(rg.getFbgczc());
                qk.setYearCostFyglJe(rg.getGlfy());
                qk.setYearCostCwfyJe(rg.getCwfy());
                qk.setYearCostQtJe(rg.getQt());
                calcZb(qk);
            }
            res.add(qk);
        }
        // 目前非全字段 有些字段未表明取值 或无法取值 且类型为String(未修改)
        // 顺序 不要变
        DataAnalysisNdgccbqkhzb zj = zj(res);
        zj.setProName("总计");
        res = xmbFz(res);
        zyfz(res);
        res.add(zj);
        return res;
    }

    private List<DataAnalysisNdgccbqkhzb> xmbFz(List<DataAnalysisNdgccbqkhzb> res) {
        List<DataAnalysisNdgccbqkhzb> newResList = new ArrayList<>();
        Map<String, List<DataAnalysisNdgccbqkhzb>> xmbGroup = res.stream().filter(e -> StrUtil.isNotBlank(e.getDeptName())).collect(Collectors.groupingBy(DataAnalysisNdgccbqkhzb::getDeptName));
        for (Map.Entry<String, List<DataAnalysisNdgccbqkhzb>> entry : xmbGroup.entrySet()) {
            String key = entry.getKey();
            List<DataAnalysisNdgccbqkhzb> value = entry.getValue();
            newResList.addAll(value);
            DataAnalysisNdgccbqkhzb zj = zj(value);
            zj.setProName(key  + "小计");
            newResList.add(zj);
        }
        return newResList;
    }

    private void calcZb(DataAnalysisNdgccbqkhzb qk) {
        // 产值盘点
        BigDecimal yearCzPdHj = qk.getYearCzPdHj();
        // 其中专业分包
        BigDecimal yearCzPdZyfb = qk.getYearCzPdZyfb();
        // 甲供材
        BigDecimal yearCzJgc = qk.getYearCzJgc();
        // 对比值
        BigDecimal basePrice = NumberUtil.add(NumberUtil.subtract(yearCzPdHj, yearCzPdZyfb), yearCzJgc);


        BigDecimal yearCostRgfJe        = qk.getYearCostRgfJe();
        BigDecimal yearCostClfJe        = qk.getYearCostClfJe();
        BigDecimal yearCostJxfJe        = qk.getYearCostJxfJe();
        BigDecimal yearCostQtzjfJe      = qk.getYearCostQtzjfJe();
        BigDecimal yearCostJjfJe        = qk.getYearCostJjfJe();
        BigDecimal yearCostJjfGlrygzJe  = qk.getYearCostJjfGlrygzJe();
        BigDecimal yearCostFbgczcJe     = qk.getYearCostFbgczcJe();
        BigDecimal yearCostFyglJe       = qk.getYearCostFyglJe();
        BigDecimal yearCostCwfyJe       = qk.getYearCostCwfyJe();
        BigDecimal yearCostQtJe         = qk.getYearCostQtJe();

        qk.setYearCostRgfZb(NumberUtil.divideUseProportion(yearCostRgfJe,2,basePrice));
        qk.setYearCostClfZb(NumberUtil.divideUseProportion(yearCostClfJe,2,basePrice));
        qk.setYearCostJxfZb(NumberUtil.divideUseProportion(yearCostJxfJe,2,basePrice));
        qk.setYearCostQtzjfZb(NumberUtil.divideUseProportion(yearCostQtzjfJe,2,basePrice));
        qk.setYearCostJjfZb(NumberUtil.divideUseProportion(yearCostJjfJe,2,basePrice));
        qk.setYearCostJjfGlrygzZb(NumberUtil.divideUseProportion(yearCostJjfGlrygzJe,2,basePrice));
        qk.setYearCostFbgczcZb(NumberUtil.divideUseProportion(yearCostFbgczcJe,2,basePrice));
        qk.setYearCostFyglZb(NumberUtil.divideUseProportion(yearCostFyglJe,2,basePrice));
        qk.setYearCostCwfyZb(NumberUtil.divideUseProportion(yearCostCwfyJe,2,basePrice));
        qk.setYearCostQtZb(NumberUtil.divideUseProportion(yearCostQtJe,2,basePrice));

        BigDecimal lre = NumberUtil.adds(yearCostRgfJe,
                yearCostClfJe,
                yearCostJxfJe,
                yearCostQtzjfJe,
                yearCostJjfJe,
                yearCostJjfGlrygzJe,
                yearCostFbgczcJe,
                yearCostFyglJe,
                yearCostCwfyJe,
                yearCostQtJe);
        qk.setLre(lre);
        qk.setLrl(NumberUtil.divideUseProportion(lre,2,basePrice));
    }


    public List<YearLjWccz> yearWccz(List<String> pmInfoIds) {
        List<YearLjWccz> resList = new ArrayList<>();
        List<JtczResultBo> yearwcczhj = yearGcQkMapper.yearwcczhj(pmInfoIds);
        Map<String, BigDecimal> yearwcczhjMap = yearwcczhj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> yearwcczsj = yearGcQkMapper.yearwcczsj(pmInfoIds);
        Map<String, BigDecimal> yearwcczsjMap = yearwcczsj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> yearwccjgc = yearGcQkMapper.yearwccjgc(pmInfoIds);
        Map<String, BigDecimal> yearwccjgcMap = yearwccjgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        for (String pc : pmInfoIds) {
            YearLjWccz wccz = new YearLjWccz();
            wccz.setPc(pc);
            wccz.setPdzhj(yearwcczhjMap.getOrDefault(pc,BigDecimal.ZERO));
            wccz.setSj(yearwcczsjMap.getOrDefault(pc,BigDecimal.ZERO));
            wccz.setJgc(yearwccjgcMap.getOrDefault(pc,BigDecimal.ZERO));
            resList.add(wccz);
        }
        return resList;
    }

    public List<RgJxD> rgjxD(List<String> pmInfoIds) {
        List<RgJxD> resList = new ArrayList<>();
        List<JtczResultBo> rgf = yearGcQkMapper.rgf(pmInfoIds);
        Map<String, BigDecimal> rgfMap = rgf.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> clf = yearGcQkMapper.clf(pmInfoIds);
        Map<String, BigDecimal> clfMap = clf.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> jxf = yearGcQkMapper.jxf(pmInfoIds);
        Map<String, BigDecimal> jxfMap = jxf.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> qtzjf = yearGcQkMapper.qtzjf(pmInfoIds);
        Map<String, BigDecimal> qtzjfMap = qtzjf.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> jjfJe = yearGcQkMapper.jjfJe(pmInfoIds);
        Map<String, BigDecimal> jjfJeMap = jjfJe.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> jjfGlgz = yearGcQkMapper.jjfGlgz(pmInfoIds);
        Map<String, BigDecimal> jjfGlgzMap = jjfGlgz.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> fbgczc = yearGcQkMapper.fbgczc(pmInfoIds);
        Map<String, BigDecimal> fbgczcMap = fbgczc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> glfy = yearGcQkMapper.glfy(pmInfoIds);
        Map<String, BigDecimal> glfyMap = glfy.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> cwfy = yearGcQkMapper.cwfy(pmInfoIds);
        Map<String, BigDecimal> cwfyMap = cwfy.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> qt = yearGcQkMapper.qt(pmInfoIds);
        Map<String, BigDecimal> qtMap = qt.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));

        for (String pc : pmInfoIds) {
            RgJxD rgJxD = new RgJxD();
            rgJxD.setPc(pc);
            rgJxD.setRgf(rgfMap.getOrDefault(pc,BigDecimal.ZERO));
            rgJxD.setClf(clfMap.getOrDefault(pc,BigDecimal.ZERO));
            rgJxD.setJxf(jxfMap.getOrDefault(pc,BigDecimal.ZERO));
            rgJxD.setQtzjf(qtzjfMap.getOrDefault(pc,BigDecimal.ZERO));
            rgJxD.setJjfJe(jjfJeMap.getOrDefault(pc,BigDecimal.ZERO));
            rgJxD.setJjfGlgz(jjfGlgzMap.getOrDefault(pc,BigDecimal.ZERO));
            rgJxD.setFbgczc(fbgczcMap.getOrDefault(pc,BigDecimal.ZERO));
            rgJxD.setGlfy(glfyMap.getOrDefault(pc,BigDecimal.ZERO));
            rgJxD.setCwfy(cwfyMap.getOrDefault(pc,BigDecimal.ZERO));
            rgJxD.setQt(qtMap.getOrDefault(pc,BigDecimal.ZERO));
            resList.add(rgJxD);
        }
        return resList;
    }

    private void zyfz(List<DataAnalysisNdgccbqkhzb> resList) {
        Map<String, List<DataAnalysisNdgccbqkhzb>> zyGroup = resList.stream().filter(e -> StrUtil.isNotBlank(e.getZy())).collect(Collectors.groupingBy(DataAnalysisNdgccbqkhzb::getZy));
        Set<Map.Entry<String, List<DataAnalysisNdgccbqkhzb>>> entries = zyGroup.entrySet();
        for (Map.Entry<String, List<DataAnalysisNdgccbqkhzb>> entry : entries) {
            String key = entry.getKey();
            List<DataAnalysisNdgccbqkhzb> value = entry.getValue();
            DataAnalysisNdgccbqkhzb zj = zj(value);
            zj.setProName(key + "-专业小计");
            resList.add(zj);
        }
    }
    private DataAnalysisNdgccbqkhzb zj(List<DataAnalysisNdgccbqkhzb> resList) {
        DataAnalysisNdgccbqkhzb re = new DataAnalysisNdgccbqkhzb();
        BigDecimal htTotal = BigDecimal.ZERO;
        BigDecimal jzmj = BigDecimal.ZERO;
        BigDecimal yearCzPdHj = BigDecimal.ZERO;
        BigDecimal yearCzPdZyfb = BigDecimal.ZERO;
        BigDecimal yearCzSj = BigDecimal.ZERO;
        BigDecimal yearCzJgc = BigDecimal.ZERO;
        BigDecimal yearCostRgfJe = BigDecimal.ZERO;
        BigDecimal yearCostClfJe = BigDecimal.ZERO;
        BigDecimal yearCostJxfJe = BigDecimal.ZERO;
        BigDecimal yearCostQtzjfJe = BigDecimal.ZERO;
        BigDecimal yearCostJjfJe = BigDecimal.ZERO;
        BigDecimal yearCostJjfGlrygzJe = BigDecimal.ZERO;
        BigDecimal yearCostFbgczcJe = BigDecimal.ZERO;
        BigDecimal yearCostFyglJe = BigDecimal.ZERO;
        BigDecimal yearCostCwfyJe = BigDecimal.ZERO;
        BigDecimal yearCostQtJe = BigDecimal.ZERO;
        BigDecimal yearCostHjJe = BigDecimal.ZERO;

        for (DataAnalysisNdgccbqkhzb qk : resList) {
            htTotal =               NumberUtil.add(qk.getHtTotal(),htTotal);
            jzmj =                  NumberUtil.add(qk.getJzmj(),jzmj);
            yearCzPdHj =            NumberUtil.add(qk.getYearCzPdHj(),yearCzPdHj);
            yearCzPdZyfb =          NumberUtil.add(qk.getYearCzPdZyfb(),yearCzPdZyfb);
            yearCzSj =              NumberUtil.add(qk.getYearCzSj(),yearCzSj);
            yearCzJgc =             NumberUtil.add(qk.getYearCzJgc(),yearCzJgc);
            yearCostRgfJe =         NumberUtil.add(qk.getYearCostRgfJe(),yearCostRgfJe);
            yearCostClfJe =         NumberUtil.add(qk.getYearCostClfJe(),yearCostClfJe);
            yearCostJxfJe =         NumberUtil.add(qk.getYearCostJxfJe(),yearCostJxfJe);
            yearCostQtzjfJe =       NumberUtil.add(qk.getYearCostQtzjfJe(),yearCostQtzjfJe);
            yearCostJjfJe =         NumberUtil.add(qk.getYearCostJjfJe(),yearCostJjfJe);
            yearCostJjfGlrygzJe =   NumberUtil.add(qk.getYearCostJjfGlrygzJe(),yearCostJjfGlrygzJe);
            yearCostFbgczcJe =      NumberUtil.add(qk.getYearCostFbgczcJe(),yearCostFbgczcJe);
            yearCostFyglJe =        NumberUtil.add(qk.getYearCostFyglJe(),yearCostFyglJe);
            yearCostCwfyJe =        NumberUtil.add(qk.getYearCostCwfyJe(),yearCostCwfyJe);
            yearCostQtJe =          NumberUtil.add(qk.getYearCostQtJe(),yearCostQtJe);
            yearCostHjJe =          NumberUtil.add(qk.getYearCostHjJe(),yearCostHjJe);
        }

        re.setHtTotal(htTotal);
        re.setJzmj(jzmj);
        re.setYearCzPdHj(yearCzPdHj);
        re.setYearCzPdZyfb(yearCzPdZyfb);
        re.setYearCzSj(yearCzSj);
        re.setYearCzJgc(yearCzJgc);
        re.setYearCostRgfJe(yearCostRgfJe);
        re.setYearCostClfJe(yearCostClfJe);
        re.setYearCostJxfJe(yearCostJxfJe);
        re.setYearCostQtzjfJe(yearCostQtzjfJe);
        re.setYearCostJjfJe(yearCostJjfJe);
        re.setYearCostJjfGlrygzJe(yearCostJjfGlrygzJe);
        re.setYearCostFbgczcJe(yearCostFbgczcJe);
        re.setYearCostFyglJe(yearCostFyglJe);
        re.setYearCostCwfyJe(yearCostCwfyJe);
        re.setYearCostQtJe(yearCostQtJe);
        re.setYearCostHjJe(yearCostHjJe);
        re.hb();
        calcZb(re);
        return re;
    }

}
